package threadpoolnio;

import threadpoolnio.server.NIOServer;
import threadpoolnio.server.NIOServer2;
import threadpoolnio.server.ReceiveMessage;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author ：xx
 * @date ：Created in 2020/2/27 20:12
 * @description：
 * @modified By：
 */
public class mainServer {
    private JPanel panel1;
    private JTextField txtServerIP;
    private JTextField a9999TextField;
    private JButton btnRun;
    private JTextArea txtResult;
    private JButton btnStop;
    private threadpoolnio.TCPServer_Thread tcpServer_thread=null;
//    private  NIOServer nioServer=null;
    private  NIOServer2 nioServer=null;
    private ReceiveMessage receiveMessage=null;


    public mainServer() {
        btnRun.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                NetPrintStream printStream=new NetPrintStream(System.out,txtResult);
                System.setErr(printStream);
                System.setOut(printStream);
//                tcpServer_thread=new TCPServer_Thread();
//                tcpServer_thread.start();
//                nioServer=new NIOServer();
                nioServer=new NIOServer2();
                nioServer.start();
                receiveMessage=new ReceiveMessage(9999);
                receiveMessage.start();
            }
        });

        btnStop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(tcpServer_thread!=null) {
                    tcpServer_thread.stopServer();
                }
                if(nioServer!=null)
                    nioServer.close();
                if(receiveMessage!=null)
                    receiveMessage.close();
            }
        });
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("mainServer");
        frame.setContentPane(new mainServer().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

    }

}
